
import { Vector3 } from "../base_frame/math/Vector3";
import { LightCube } from "./LightCube";

export class LightCube02 extends LightCube{


  
    protected readonly VS: string =
        "attribute vec4 a_Position;\n" +
        "attribute vec4 a_Color;\n" +
        "attribute vec4 a_Normal;\n" +
        "uniform mat4 u_MvpMatrix;\n" +
        "uniform mat4 u_NormalMatrix;\n" +
        "uniform vec3 u_LightPosition;\n" +
        "uniform vec3 u_LightColor;\n" +
        "uniform vec3 u_AmbientColor;\n" +
        "varying vec4 v_Color;\n" +
        "void main(){\n" +
        "   vec3 normal = normalize((u_NormalMatrix * a_Normal).xyz);\n" +
        "   vec3 lightDir = normalize(u_LightPosition - a_Position.xyz);\n" +
        "   vec3 diffColor = max(dot(lightDir, normal),0.0) * u_LightColor * a_Color.rgb;\n" + // 漫反射
        "   vec3 ambientColor = u_AmbientColor * a_Color.rgb;\n" + // 环境光
        "   v_Color = vec4(diffColor+ambientColor,a_Color.a);\n" + // 反射光//vec4(ambientColor + diffColor, a_Color.a);
        "   gl_Position = u_MvpMatrix * a_Position;\n" + // 反射光  // 没有引用到的属性值会被剔除掉
        "}\n";

    protected readonly FS: string =
        "precision mediump float;\n" +
        "varying vec4 v_Color;\n" +
        "void main(){\n" +
        "   gl_FragColor = v_Color;\n" +
        "}\n";

    private u_LightPosition:WebGLUniformLocation;
    private lightPosition = new Vector3(3.0,2.2,0.0);
    initValue(gl: WebGL2RenderingContext | WebGLRenderingContext) {
        let vec3 = this.lightPosition;
        gl.uniform3f(this.u_LightPosition, vec3.x, vec3.y, vec3.z);
        gl.uniform3f(this.u_LightColor, 1.0, 1.0, 1.0);
        gl.uniformMatrix4fv(this.u_NormalMatrix, false, this.normalMatrix.elements);
        gl.uniform3f(this.u_AmbientColor, 0.2, 0.2, 0.2);
    }

    initRef(gl: WebGL2RenderingContext | WebGLRenderingContext): void {
        //@ts-ignore
        let program = gl.program;
        this.a_Position = gl.getAttribLocation(program, "a_Position");
        this.a_Color = gl.getAttribLocation(program, "a_Color");
        this.a_Normal = gl.getAttribLocation(program, "a_Normal");
        this.u_LightPosition = gl.getUniformLocation(program, "u_LightPosition");
        this.u_LightColor = gl.getUniformLocation(program, "u_LightColor");
        this.u_AmbientColor = gl.getUniformLocation(program, "u_AmbientColor");
        this.u_NormalMatrix = gl.getUniformLocation(program, "u_NormalMatrix");
    }


}